package rent.hj.videoplayer;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import rent.hj.videoplayer.R;

/**
 * Created by wangjianqiang on 2016/6/16.
 */
public class VideoProgressFullScreen extends FrameLayout {
    private View progressView;
    private Context context;
    private RelativeLayout rl_2d;
    private LinearLayout   ll_3d;
    private boolean is3DFormat = false;
    private VideoPlayActivity mActivity;
    private boolean isShowing = false;

    private ProgressBar progress,progress_3d_left,progress_3d_right;
    public VideoProgressFullScreen(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public VideoProgressFullScreen(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    public VideoProgressFullScreen(Context context) {
        super(context);
        this.context = context;
        init();
    }
    private void init(){
        progressView = inflate(getContext(), R.layout.video_full_screen_progress,this);
        rl_2d        = (RelativeLayout)progressView.findViewById(R.id.video_full_screen_progress_2d);
        ll_3d        = (LinearLayout)progressView.findViewById(R.id.video_full_screen_progress_3d);
        progress     = (ProgressBar)progressView.findViewById(R.id.mediacontroller_progress_full_screen);
        progress_3d_left = (ProgressBar)progressView.findViewById(R.id.mediacontroller_progress_full_screen_3d_left);
        progress_3d_right = (ProgressBar)progressView.findViewById(R.id.mediacontroller_progress_full_screen_3d_right);

        progress.setMax(1000);
        progress_3d_left.setMax(1000);
        progress_3d_right.setMax(1000);
    }

    public void setmActivity(VideoPlayActivity mActivity){
        this.mActivity = mActivity;
    }

    public void setIs3DFormat(boolean is3DFormat) {
        this.is3DFormat = is3DFormat;
        if(!isShowing){
            return;
        }
        if (is3DFormat) {
            rl_2d.setVisibility(View.GONE);
            ll_3d.setVisibility(View.VISIBLE);
        } else {
            rl_2d.setVisibility(View.VISIBLE);
            ll_3d.setVisibility(View.GONE);
        }

    }
    public void show(){
        if (is3DFormat) {
            rl_2d.setVisibility(View.GONE);
            ll_3d.setVisibility(View.VISIBLE);
        } else {
            rl_2d.setVisibility(View.VISIBLE);
            ll_3d.setVisibility(View.GONE);
        }
        isShowing = true;
        mHandler.removeMessages(SHOW_PROGRESS);
        mHandler.sendEmptyMessage(SHOW_PROGRESS);
    }
    public void hide(){
        rl_2d.setVisibility(View.GONE);
        ll_3d.setVisibility(View.GONE);
        mHandler.removeMessages(SHOW_PROGRESS);
        isShowing = false;
    }
    private static final int SHOW_PROGRESS = 0;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int pos;
            switch (msg.what) {
                case SHOW_PROGRESS:

                    pos = setProgress();
                    msg = obtainMessage(SHOW_PROGRESS);
                    if (mActivity.isPlaying()) {

                        int dis = 1000 - (pos % 1000);
                        if(dis<500){
                            dis = 500;
                        }
                        sendMessageDelayed(msg,dis);
                    }else{
                        sendMessageDelayed(msg,1000);
                    }
                    break;
            }
        }
    };
    private int setProgress() {
        if (mActivity == null) {
            return 0;
        }
        int position = mActivity.getCurrentPosition();
        int duration = mActivity.getDuration();
        if (progress != null) {
            if (duration > 0) {
                long pos = 1000L * position / duration;

                progress.setProgress((int) pos);
                progress_3d_left.setProgress((int) pos);
                progress_3d_right.setProgress((int) pos);
            }

        }
        return position;
    }
    public void initProgress(){
        progress.setProgress(0);
        progress_3d_left.setProgress(0);
        progress_3d_right.setProgress(0);
        mHandler.removeMessages(SHOW_PROGRESS);
    }
}
